# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/3/4
# @File   : EC06_ExtGuarLoanCnt.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:对外担保笔数
code:EC06_ExtGuarLoanCnt
tableName:
columnName:
groups:企业征信模块
dependencies:EC_DSJ
type:常用指标
dataSourceType:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = EC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['ReportNo'] in null_type_list:
                return [{}]
            # 存在表 但为空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def EC06_ExtGuarLoanCnt():
    try:
        ECRSummaryGuarantee = getTable('ECRSummaryGuarantee')
        if ECRSummaryGuarantee is None:
            return u'缺失值'
        elif ECRSummaryGuarantee == [{}]:
            return 0.0
        else:
            flag = []
            bz_amount = 0
            dy_amount = 0
            zy_amount = 0
            for i in ECRSummaryGuarantee:
                count_flag = 0
                if i.get('Guaranteetype') == u'保证汇总' and i.get('ReportNo') not in null_type_list:
                    amount = i.get('Amount')
                    try:
                        amount = float(amount)
                    except:
                        amount = 0
                        count_flag += 1

                    if count_flag == 1:
                        bz_amount = None
                    else:
                        bz_amount = amount

                elif i.get('Guaranteetype') == u'抵押汇总' and i.get('ReportNo') not in null_type_list:
                    amount = i.get('Amount')
                    try:
                        amount = float(amount)
                    except:
                        amount = 0
                        count_flag += 1
                    if count_flag == 1:
                        dy_amount = None
                    else:
                        dy_amount = amount

                elif i.get('Guaranteetype') == u'质押汇总' and i.get('ReportNo') not in null_type_list:
                    amount = i.get('Amount')
                    try:
                        amount = float(amount)
                    except:
                        amount = 0
                        count_flag += 1

                    if count_flag == 1:
                        zy_amount = None
                    else:
                        zy_amount = amount
                else:
                    flag.append(None)

            if dy_amount == None and zy_amount == None and bz_amount == None:
                return u'缺失值'
            elif len(flag) == len(ECRSummaryGuarantee):
                return u'缺失值'
            else:
                if dy_amount is None:
                    dy_amount = 0
                if zy_amount is None:
                    zy_amount = 0
                if bz_amount is None:
                    bz_amount = 0
                return dy_amount + zy_amount + bz_amount
    except:
        return u'缺失值'

result = EC06_ExtGuarLoanCnt()